<script>
/**
 * 文件上传表单域
 *    props: {
 *      url: string,
 *      value: string,
 *      uploader: <el-upload>
 *      completeLabel: string,
 *      preloadLabel: string,
 *      ...el-button
 *   }
 */
export default {
  name: 'YmFileUploader',
  functional: true,
  render (h, context) {
    const { props = {}, listeners = {} } = context
    const label = (props.value) ? (props.completeLabel || '已上传') : (props.preloadLabel || '点击上传')
    const success = (response) => {
      if (response.status !== false) {
        if (listeners.change) (listeners.change)(response.url)
      }
    }
    return h(
      'el-upload',
      {
        style: {
          float: 'left'
        },
        props: {
          action: props.url || '',
          'show-file-list': false,
          'on-success': success,
          ...(props.uploader || {})
        }
      },
      [
        h('el-button', {
          props: {
            type: 'primary'
          }
        }, label)
      ]
    )
  }
}
</script>
